// 导入 pinia
import { defineStore } from 'pinia'
// 导入api
import { getEchart, type EchartDataModel } from '@/api/echart'
import { ElMessage } from 'element-plus'

// 初始化数据
const initEchartData = (): EchartDataModel => ({
  '//1': '今日销售额',
  salesToday: 0,
  salesLastDay: 0,
  salesGrowthLastDay: 0,
  salesGrowthLastMonth: 0,

  '//2': '今日订单量',
  orderToday: 0,
  orderLastDay: 0,
  orderTrend: [],
  orderTrendAxis: [],

  '//3': '今日交易用户数',
  orderUser: 0,
  returnRate: 0,
  orderUserTrend: [],
  orderUserTrendAxis: [],

  '//4': '累计用户数',
  usersTotal: 0,
  usersLastDay: 0,
  usersLastMonth: 0,
  userGrowthLastDay: '',
  userGrowthLastMonth: '',

  '//5': '销售额与访问量',
  orderFullYearAxis: [],
  orderFullYear: [],
  userFullYearAxis: [],
  userFullYear: [],

  '//6': '排行榜',
  orderRank: [],
  userRank: [],

  '//7': '关键字搜索',
  searchWord: [],

  '//8': '分类销售排行',
  saleRank: {
    category: {
      axisX: [],
      data1: []
    },
    goods: {
      axisX: [],
      data1: []
    }
  }
})

// 创建仓库
const useEchartStore = defineStore('echartStore', {
  state: () => ({
    // 存储数据
    storeEchartData: initEchartData()
  }),
  actions: {
    // 调用接口获取数据
    async getEchartdata() {
      try {
        const res = await getEchart()
        // 存储数据
        this.storeEchartData = res.data
      } catch (error) {
        // 提示
        ElMessage.error('获取图表数据失败,请重试')
        // 将错误继续向下抛出
        return Promise.reject(error)
      }
    }
  }
})

// 抛出
export default useEchartStore
